﻿using CsharpHttpHelper;
using CsharpHttpHelper.Enum;
using Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace HTTP
{
    class HTTP
    {
        public HttpResult ExecuteHTTP(RequestParameteData rpd, string body)
        {
            
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = bulidUrl(rpd.requestUrlget,rpd.requestUrl),//URL     必需项    
                Method = rpd.requestType.ToLower(),//URL     可选项 默认为Get
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写    
                Cookie = buildCookie(rpd.requestCookie),//字符串Cookie     可选项    
                Referer = bulidHead(rpd.requestHeads, "Referer"),//来源URL     可选项     
                //PostDataType = PostDataType.
                Postdata = body,//DifferentTypeNodeToJson(),//PostData
                Timeout = rpd.requestWriteOutTime,//连接超时时间毫秒     可选项默认为100000    
                ReadWriteTimeout = rpd.requestReadOutTime,//写入Post数据超时时间毫秒      可选项默认为30000                                    
                UserAgent = bulidHead(rpd.requestHeads, "UserAgent"),//用户的浏览器类型，版本，操作系统     可选项有默认值    
                Accept = bulidHead(rpd.requestHeads, "Accept"),//    可选项有默认值    
                ContentType = bulidHead(rpd.requestHeads, "ContentType"),//返回类型    可选项有默认值    
                Allowautoredirect = false,//是否根据301跳转     可选项   
                ProtocolVersion = bulidHead(rpd.requestHeads, "ProtocolVersion") == "HTTP/1.1" ? System.Net.HttpVersion.Version11 :
                bulidHead(rpd.requestHeads, "ProtocolVersion") == "HTTP/1.0" ? System.Net.HttpVersion.Version10 : System.Net.HttpVersion.Version11,//HTTP版本
                Encoding = Encoding.UTF8,//编码方式                
                PostEncoding = Encoding.UTF8,//Post 数据编码
                Header = bulidHead(rpd.requestHeads),//头信息
                ResultType = CsharpHttpHelper.Enum.ResultType.String
            };

            //文件上传
            if (rpd.requestForm.Count > 0 )
            {
                item.PostDataType = PostDataType.Byte;
                item.PostdataByte = new FormData().build(rpd.requestForm);
                item.ContentType = "multipart/form-data; boundary=" + new FormData().getBoundStr().Substring(2);
                item.Postdata = null;
            }
            HttpResult result = http.GetHtml(item);
            return result;
        }



        /// <summary>
        /// 获取URL信息
        /// </summary>
        /// <param name="head"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public string bulidUrl(List<ParameterItemNodeData> urlget,string urls)
        {
            string url = "";
            foreach (ParameterItemNodeData pind in urlget)
            {
                url = pind.Key + "=" + pind.Value + "&";
            }
            return urls + "?" + url.TrimEnd(new char[] { '&' });
        }

        /// <summary>
        /// 获取头信息
        /// </summary>
        /// <param name="head"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public string bulidHead(List<ParameterItemNodeData> head, string name)
        {
            ParameterItemNodeData lpi = head.Find(x => x.Key == name);
            return lpi.Value;
        }
        
        /// <summary>
        /// 构建head
        /// </summary>
        /// <param name="head"></param>
        /// <returns></returns>
        public WebHeaderCollection bulidHead(List<ParameterItemNodeData> head)
        {
            System.Net.WebHeaderCollection header = new System.Net.WebHeaderCollection();
            foreach (ParameterItemNodeData pind in head)
            {
                if (!pind.Type.Equals("SystemHeads"))
                {
                    try
                    {
                        header.Add(pind.Key, pind.Value);
                    }
                    catch (System.ArgumentException e)
                    { }
                }
                //header.Add(pind.Key, pind.Value);
            }
            return header;
        }

        /// <summary>
        /// 构建cookie
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string buildCookie(List<ParameterItemNodeData> cookie)
        {
            string _cookie = "";
            foreach (ParameterItemNodeData pind in cookie)
            {
                _cookie = pind.Key + "=" + pind.Value + ";";
            }
            return _cookie.TrimEnd(new char[] { ';' });
        }

    }
}